package org.gongliang.jsmile.common.base;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * crud基本服务类
 * 
 * @author MINI
 *
 */
public abstract class CRUDService<M extends BaseDao<T>, T> implements BaseService<T> {

	@Autowired
	protected M baseDao;

	/**
	 * 是否操作成功
	 */
	public static boolean retBool(Integer result) {
		return (null == result) ? false : result >= 1;
	}

	/**
	 * 查询所有数据
	 */
	@Override
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public List<T> findAll(Map<String, Object> columnMap) {
		return baseDao.findAll(columnMap);
	}

	/**
	 * 根据id查询数据
	 */
	@Override
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public T findById(T entity) {
		return baseDao.findById(entity);
	}

	/**
	 * 添加一条数据
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean saveOne(T entity) {
		return retBool(baseDao.saveOne(entity));
	}

	/**
	 * 根据id修改一条数据
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean updateById(T entity) {
		return retBool(baseDao.updateById(entity));
	}

	/**
	 * 根据id删除一条数据
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean deleteById(T entity) {
		return retBool(baseDao.deleteById(entity));
	}

	/**
	 * 根据ids数组批量删除数据
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean deleteByIds(String[] ids) {
		return retBool(baseDao.deleteByIds(ids));
	}
}
